import { Controller, Post, Body, Get } from "@nestjs/common";
import { ApiRes, UsePermGuard, definePermission } from "src/decorator";
import { ApiOperation, ApiTags } from "@nestjs/swagger";
import { RoleEntity } from "src/entity";
import { RoleService } from "../service/role.service";
import { RoleAddDto, RoleQueryDto, RoleRemoveDto, RoleUpdateDto } from "src/dto/role";
import { Perm as AdminPerm } from "src/user/controller/admin.controller";

export const Perm = definePermission("role", {
    query: {
        name: "查询",
        code: "query",
        description: "角色查询权限",
    },
    add: {
        name: "添加",
        code: "add",
        description: "角色添加权限",
    },
    update: {
        name: "更新",
        code: "update",
        description: "角色更新权限",
    },
    remove: {
        name: "移除",
        code: "remove",
        description: "角色移除权限",
    },
} as const);

@ApiTags("Role - 模块")
@Controller("role")
export class RoleController {
    constructor(private readonly roleService: RoleService) {}

    @ApiOperation({ summary: "角色列表" })
    @ApiRes({ type: RoleEntity, pagination: true })
    @UsePermGuard(Perm.QUERY, AdminPerm.ADD, AdminPerm.UPDATE)
    @Post("query")
    query(@Body() dto: RoleQueryDto) {
        return this.roleService.query(dto);
    }

    @ApiOperation({ summary: "添加角色" })
    @ApiRes({ type: RoleEntity })
    @UsePermGuard(Perm.ADD)
    @Post("add")
    add(@Body() dto: RoleAddDto) {
        return this.roleService.add(dto);
    }

    @ApiOperation({ summary: "更新角色" })
    @UsePermGuard(Perm.UPDATE)
    @Post("update")
    update(@Body() dto: RoleUpdateDto) {
        return this.roleService.update(dto);
    }

    @ApiOperation({ summary: "移除角色" })
    @UsePermGuard(Perm.REMOVE)
    @Post("remove")
    remove(@Body() dto: RoleRemoveDto) {
        return this.roleService.remove(dto.id);
    }
}
